package com.pakazaka.map;

import com.pakazaka.layer.TiledLayer;
import com.pakazaka.layer.tiles.Tile;
import com.pakazaka.layer.tiles.TilesDef;
import com.pakazaka.simplecrawler.SimpleCrawler;
import com.pakazaka.testItemsSystem.Item;
import com.pakazaka.testItemsSystem.ItemPrototype;
import com.pakazaka.testItemsSystem.TestItemEnum;
import java.util.ArrayList;
import java.util.Random;

/**
 * @author Remi
 */
public class SimpleGenerate {

    private static final Random rnd = new Random();

    public static void generateMap(TiledLayer layer, ArrayList<Item> resources) throws CloneNotSupportedException {
        resources.clear();
        Tile[][] map = layer.getMap();

        fillObjectRect(map, TilesDef.grass, 0, 0, SimpleCrawler.mapw, SimpleCrawler.maph);
        drawObjectRect(map, TilesDef.invincibleWall, 0, 0, SimpleCrawler.mapw - 1, SimpleCrawler.maph - 1);

        //house 1
        fillObjectRect(map, TilesDef.houseFloor, 5, 5, 23, 23);
        drawObjectRect(map, TilesDef.houseWall, 5, 5, 23, 23);

        //house 2
        fillObjectRect(map, TilesDef.houseFloor, 32, 8, 12, 16);
        drawObjectRect(map, TilesDef.houseWall, 32, 8, 12, 16);
        setTile(map, TilesDef.houseDoor, 32, 16);

        setTile(map, TilesDef.houseDoor, 7, 28);
        setTile(map, TilesDef.houseDoor, 18, 5);

        setTile(map, TilesDef.houseDoor, 5, 21);

        //THE bydlocode
        for (int i = 0; i < 30; i++) {
            int id = rnd.nextInt(6);

            int x = rnd.nextInt(23) + 5;
            int y = rnd.nextInt(23) + 5;

            if (!layer.getTileObject(x, y).canWalk()) {
                continue;
            }
            TestItemEnum itemId;
            switch (id) {
                case 0:
                    itemId = TestItemEnum.GLASS_SHARDS;
                    break;
                case 1:
                    itemId = TestItemEnum.HAMMER;
                    break;
                case 2:
                    itemId = TestItemEnum.COLA_CAN;
                    break;
                case 3:
                    itemId = TestItemEnum.CANDY;
                    break;
                case 4:
                    itemId = TestItemEnum.LEATHER_BOOTS;
                    break;
                case 5:
                    itemId = TestItemEnum.LEATHER_HAT;
                    break;
                case 6:
                    itemId = TestItemEnum.WATER_BOTTLE_500;
                    break;
                case 7:
                    itemId = TestItemEnum.SCISSORS;
                    break;
                case 8:
                    itemId = TestItemEnum.BOOK;
                    break;
                case 9:
                    itemId = TestItemEnum.MEDKIT;
                    break;
                case 10:
                    itemId = TestItemEnum.PILLS;
                    break;
                case 11:
                    itemId = TestItemEnum.LIGHTER;
                    break;
                default:
                    itemId = TestItemEnum.COLA_CAN;
            }
            resources.add(new Item(itemId.name(), ItemPrototype.getInstance().getTypeById(itemId.name()), x, y));
        }
    }

    //THE lifehack
    public static void testMethod(int... a) {
    }

    private static void drawRect(Tile[][] map, int tile, int x, int y, int w, int h) {
        for (int i = x; i < x + w + 1; i++) {
            map[i][y].setId(tile);
            map[i][y + h].setId(tile);
        }
        for (int i = y; i < y + h + 1; i++) {
            map[x][i].setId(tile);
            map[x + w][i].setId(tile);
        }
    }

    private static void fillRect(Tile[][] map, int tile, int x, int y, int w, int h) {
        for (int i = x; i < x + w; i++) {
            for (int j = y; j < y + h; j++) {
                map[i][j].setId(tile);
            }
        }
    }

    private static void fillObjectRect(Tile[][] map, Tile tile, int x, int y, int w, int h) throws CloneNotSupportedException {
        for (int i = x; i < x + w; i++) {
            for (int j = y; j < y + h; j++) {
                map[i][j] = tile.clone();
            }
        }
    }

    private static void drawObjectRect(Tile[][] map, Tile tile, int x, int y, int w, int h) throws CloneNotSupportedException {
        for (int i = x; i < x + w + 1; i++) {
            map[i][y] = tile.clone();
            map[i][y + h] = tile.clone();
        }
        for (int i = y; i < y + h + 1; i++) {
            map[x][i] = tile.clone();
            map[x + w][i] = tile.clone();
        }
    }

    private static void setTile(Tile[][] map, Tile tile, int x, int y) throws CloneNotSupportedException {
        map[x][y] = tile.clone();
    }

    //ГЕНИРАТЕАРОВАТЬ ТЕСТОВУЮ МКАРТУ ВЕРСИЮЬ НОЛЬ ТОЧКА ОЛИДН ОЧЕНЬ КРУТО ПОПРОЬУЕЙ ЕЩЁ ЗАХОЧЕШ
    private int[][] generateTestMapVersion_0dot1() {
        return new int[64][64];
    }
    //end
}
